﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_DistributionCompany_DistributionCompany : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(),Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);

        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";
        if (!IsPostBack)
        {
            if (clsAuth.IsAddAuth() == false)
                btnAdd_Catalogue.Visible = false;       
            BinData();           
        }
    }
    protected void btnAdd_Catalogue_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddDistributionCompany.aspx?CodeID=" + Request.QueryString["CodeID"]);
    }

     

  
    private void BinData()
    {
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("DisCompID","0");
        msg.CreateFirstNode("Name", txtName.Text.Trim());
        msg.CreateFirstNode("Phone", txtPhone.Text.Trim());
        msg.CreateFirstNode("Email", txtEmail.Text.Trim());
        msg.CreateFirstNode("Address", txtAddress.Text.Trim());
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_DistributionCompanyByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells.RemoveAt(1);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");

            //Truong hop xoa
            if (pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grv_Master.EditIndex = e.NewEditIndex;
        BinData();
    }
   

    protected void grv_Master_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        cls.UpdateData("Delete from  DistributionCompany where DisCompID=" + grv_Master.DataKeys[e.RowIndex].Value.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BinData();
    }
    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        BinData();
    }
    protected void ddlChuDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BinData();
    }
}
